
#ifndef __CAMERA_CONTROLLER_H__
#define __CAMERA_CONTROLLER_H__

#include "cocos2d.h"
#include "Box2D/Box2D.h"


#include <vector>

using namespace cocos2d;

#define PTM_RATIO 32

class CameraController;

class LooppingLayer : public CCLayer
{
public:
	CameraController* cam;
	bool loopX;
	bool loopY;

	CCRect Bound;

	LooppingLayer(bool _x = true, bool _y = false, CCRect _b = CCRect(0.0f,0.0f,480.0f,320.0f) , CameraController* __cam = NULL ) :
		CCLayer()
		{
			cam = __cam;
			loopX = _x;
			loopY = _y;
			Bound = _b;
		};

	~LooppingLayer(){};


	virtual void visit();
	
	//virtual void draw();
};

///

class ParralaxLayer
{
public:
	CCLayer* layer;
	CCPoint speedCoef;

	ParralaxLayer(CCLayer* _layer = NULL, CCPoint _speedCoef = CCPoint(0.0f,0.0f) )
	{
		layer = _layer; 
		speedCoef = _speedCoef;
	}
};

///

class CameraController
{
	enum PARRALAX_TAG
	{
		PARRALAX_TAG_0 = 0,
		PARRALAX_TAG_1,
		PARRALAX_TAG_2,
		PARRALAX_TAG_3,
		PARRALAX_TAG_4,

		MAX_PARRALAX_LAYER
	};

	CCLayer* mCameraLayer;
	ParralaxLayer mParralaxLayers[MAX_PARRALAX_LAYER];

	CCPoint mCamPos;
	bool bPosDirty;

	CCNode* mTarget;
	CCRect mUnfollowZone;

public:

	CameraController();
	virtual ~CameraController();

	CCLayer* init( CCLayer* _layerCamera = NULL );
	CCLayer* addParralaxLayer(  CCPoint& _coefSpeed, bool _loopX = true, bool _loopY = true, CCRect _rect = CCRect(0.0f,0.0f, 480.0f, 320.0f) , CCLayer* _layer = NULL );

	void followTarget();
	void RefreshPosition();
	void update( ccTime dt );



	// utils
	CCLayer* getParralaxLayer( int _id );

	void setUnfollowZone(CCRect _rect) { mUnfollowZone = _rect; }
	void setTarget(CCNode* _node) { mTarget = _node; }

	CCLayer* getCamLayer() {return mCameraLayer;}

	void setCamPos( b2Vec2& _pos);
	void setCamPos( CCPoint& _pos);
	void setCamPos( float x, float y);

	CCPoint getCamPos() {return mCamPos;}

	CCPoint getScreenPos( CCNode* _node );
	CCPoint getScreenPos( CCPoint& _pos );

	b2Vec2 getScreenPos( b2Body* __body );
	b2Vec2 getScreenPos( const b2Vec2& _pos );
};

#endif